﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Domain
{
    /// <summary>
    /// Участник гонки
    /// </summary>
    public class Racer: INotifyPropertyChanged
    {
        #region Fields
        
        private String _firstName;

        private String _secondName;

        private String _middleName;

        private DateTime _birthday;

        private City _city;

        private Category _category;

        #endregion

        #region Properties
        
        /// <summary>
        /// Имя
        /// </summary>
        public String FirstName
        {
            get { return _firstName; }
            set
            {
                bool changed = value != _firstName;
                _firstName = value;
                if (changed) InvokePropertyChanged(new PropertyChangedEventArgs("FirstName"));
            }
        } 

        /// <summary>
        /// Фамилия
        /// </summary>
        public String SecondName
        {
            get { return _secondName; }
            set
            {
                bool changed = value != _secondName;
                _secondName = value;
                if (changed) InvokePropertyChanged(new PropertyChangedEventArgs("SecondName"));
            }
        } 

        /// <summary>
        /// Отчество
        /// </summary>
        public String MiddleName
        {
            get { return _middleName; }
            set
            {
                bool changed = value != _middleName;
                _middleName = value;
                if (changed) InvokePropertyChanged(new PropertyChangedEventArgs("MiddleName"));
            }
        } 
        
        /// <summary>
        /// Дата рождения
        /// </summary>
        public DateTime Birthday
        {
            get { return _birthday; }
            set
            {
                bool changed = value != _birthday;
                _birthday = value;
                if (changed) InvokePropertyChanged(new PropertyChangedEventArgs("Birthday"));
            }
        } 

        /// <summary>
        /// Город, откуда приехал участник
        /// </summary>
        public City City
        {
            get { return _city; }
            set
            {
                bool changed = value != _city;
                _city = value;
                if (changed) InvokePropertyChanged(new PropertyChangedEventArgs("City"));
            }
        } 

        /// <summary>
        /// Категория, в которой зарегистрирован участник
        /// </summary>
        public Category Category
        {
            get { return _category; }
            set
            {
                bool changed = value != _category;
                Category oldCategory = _category;
                _category = value;
                if (changed)
                {
                    InvokePropertyChanged(new PropertyChangedEventArgs("Category"));
                    if (oldCategory != null)
                        if (oldCategory.Racers.Contains(this))
                            oldCategory.Racers.Remove(this);

                    if (_category != null)
                        if (!_category.Racers.Contains(this))
                            _category.AddRacer(this);
                }

            }
        }

        /// <summary>
        /// Результаты заездов
        /// </summary>
        public Dictionary<Run, RunResult> RaceResult { get; private set; }

        #endregion

        public Racer()
        {
            RaceResult = new Dictionary<Run, RunResult>();
        }

        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        public void InvokePropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, e);
        }

        #endregion
    }
}
